package com.example.hybridmobiledemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTabHost;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.example.hybridmobiledemo.databinding.ActivityMainBinding;
import com.example.hybridmobiledemo.module.main.fragment.MainFragment;
import com.example.hybridmobiledemo.module.main.model.news.search.SearchActivity;
import com.example.hybridmobiledemo.module.my.fragment.MyFragment;
import com.example.hybridmobiledemo.module.test.fragment.TestFragment;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding mainBinding;
    private FragmentTabHost tab;
    //  底边栏tab资源
    private String[] tabs = new String[]{"首页", "创意设计", "我的"};
    private Class[] mFragmentClasses = new Class[]{MainFragment.class,
            TestFragment.class, MyFragment.class};
    private int[] selectorImg = new int[]{R.drawable.main,R.drawable.test, R.drawable.profile_light};
    private int currentTab;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mainBinding.getRoot());
        initView();
    }

    private void initView() {
        tab = (FragmentTabHost) mainBinding.tab;
//        默认tab显示
        currentTab = getIntent().getIntExtra("CurrentTab", 0);
        initTab();
        tabChange();
    }

    private void initTab() {
        tab.setup(this, getSupportFragmentManager(), R.id.fragment_content);
        for (int i = 0; i < 3; i++) {
           tab.addTab(tab.newTabSpec(tabs[i]).setIndicator(getTabView(i)), mFragmentClasses[i], null);
        }
        tab.setCurrentTab(currentTab);
        initTabIndex();
        tab.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                tabChange();
            }
        });
    }

    private View getTabView(int i) {
        View inflate = LayoutInflater.from(this).inflate(R.layout.item_tab, null);
        ImageView tabImage = inflate.findViewById(R.id.tab_image);
        TextView tabTitle = inflate.findViewById(R.id.tab_title);
        tabImage.setImageResource(selectorImg[i]); // 通过selector来控制图片的改变
        tabTitle.setText(tabs[i]);// 通过selector来控制文字颜色的改变
        return inflate;
    }

    private void tabChange() {
        int tabIndex = tab.getCurrentTab();
        for (int i = 0; i < 3; i++) {
            View view =  tab.getTabWidget().getChildAt(i);
            ImageView ivTab = view.findViewById(R.id.tab_image);
            if (i == tabIndex) {
                ivTab.setColorFilter(Color.GREEN);
            } else {
                ivTab.setColorFilter(Color.BLACK);
            }
            initTabIndex();
        }
    }

//    根据不同tab设置标题栏
    private void initTabIndex() {
        if (tab.getCurrentTab() == 0) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mian_title_bar);
            initTitleBar();
        } else {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
        }
    }

//    标题栏初始化
    private void initTitleBar() {
        TextView tvMainTitleBarText = findViewById(R.id.tvMainTitleBarText);
        tvMainTitleBarText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                打开搜索具体页面
                startActivity(new Intent(MainActivity.this, SearchActivity.class));
            }
        });
    }
}